package hcQuest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

import webExperiment.ParticipantPanel;
import xmlParser.QDParser;
import xmlParser.XMLHandler;

public class Labels {
    XMLHandler xml = new XMLHandler();
    String[] Q = new String[13 + 1];
    String theLanguage;
    ParticipantPanel.Labels lb = new ParticipantPanel.Labels();

    String countInstruction;
    String handInstruction;

    public String endText;
    String left;
    String right;
    String either;

    public Labels() {
    }

    private String conv(String str) {
	str = str.replace("{br}", "\n");
	str = str.replace("{BR}", "\n");
	return str;
    }

    public void readXMLFile(URL fileURL) {
	final Charset charset = Charset.forName("UTF-8");
	try {
	    InputStream in = fileURL.openStream();
	    InputStreamReader reader = new InputStreamReader(in, charset);
	    BufferedReader bf = new BufferedReader(reader);

	    try {
		QDParser.parse(xml, bf);
	    } catch (Exception e) {
		System.out.println("Hello, Earthling");
		// TODO error handling
	    }
	    ;
	    bf.close();
	    reader.close();
	    in.close();
	} catch (IOException e) {
	    // xx error handling
	    xml.tagWasNotFound = true;
	    return;
	}

	writeLabels();
    };

    private void writeLabels() {
	xml.tagWasNotFound = false;
	theLanguage = conv(xml.getText("HCQ_language"));
	lb.yearOfBirth = conv(xml.getText("yearOfBirth"));
	lb.initials = conv(xml.getText("initials"));
	lb.mtongue = conv(xml.getText("mtongue"));
	lb.countryOfBirth = conv(xml.getText("countryOfBirth"));
	// lb.cityOfBirth = xml.getText("cityOfBirth");
	// lb.residence = xml.getText("residence");
	lb.gender = conv(xml.getText("gender"));
	lb.female = conv(xml.getText("female"));
	lb.male = conv(xml.getText("male"));

	left = conv(xml.getText("left"));
	right = conv(xml.getText("right"));
	either = conv(xml.getText("either"));

	endText = conv(xml.getText("endText"));

	countInstruction = conv(xml.getText("countInstruction"));
	handInstruction = conv(xml.getText("handInstruction"));

	int x;
	for (x = 1; x <= 13; x++) {
	    Q[x] = xml.getText("Q" + x);
	}
    }

} // end labeling class

